package net.sppan.base.controller.department;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import net.sppan.base.common.JsonResult;
import net.sppan.base.common.PageBean;
import net.sppan.base.controller.BaseController;
import net.sppan.base.entity.Department;
import net.sppan.base.entity.Employee;
import net.sppan.base.service.DeparmentService;
import net.sppan.base.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Controller
@RequestMapping("/department")
public class DepartmentController extends BaseController{
	@Autowired
	private DeparmentService deparmentService;
	@RequestMapping(value ="/index")
	public String index(){
		return "department/index";
	}

    /**
     * @Description:获取职工列表数据
     * @Param:
     * @return:
     */
    @RequestMapping(value = { "/getDepartmentList" })
	@ResponseBody
	public PageBean getEmployeeList(@RequestParam("pageSize") Integer pageSize, @RequestParam("pageNumber")Integer pageNumber,
                                    @RequestParam(value = "searchText", required = false)String searchText, @RequestParam(value ="sortOrder",required = false)String sortOrder) {
        Page<Employee> page = PageHelper.startPage(pageNumber, pageSize, true);
        List<Department> resultList = deparmentService.getDepartmentList();
		PageBean<Department> pageBean = new PageBean<>(page.getPageNum(), page.getPageSize(), (int) page.getTotal());
        pageBean.setContent(resultList);
		return pageBean;
	}
    /**
     * @Description:弹出添加框
     * @Param:
     * @return:
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModelMap map) {
        return "department/form";
    }
    /**
     * @Description:编辑保存
     * @Param:
     * @return:
     */
    @RequestMapping(value= {"/edit"} ,method = RequestMethod.POST)
    @ResponseBody
    public JsonResult edit(Department department, ModelMap map){
        try {
            deparmentService.saveDepartment(department);
        } catch (Exception e) {
            return JsonResult.failure(e.getMessage());
        }
        return JsonResult.success();
    }
    /**
     * @Description: 编辑数据回显
     * @Param:
     * @return:
     * @Author:
     */
    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String edit(@PathVariable Integer id, ModelMap map) {
        Department department = deparmentService.getDepartment(id);
        map.put("department", department);
        return "department/form";
    }
    /**
     * @Description:删除职工
     * @Param:
     * @return:
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult delete(@PathVariable Integer id,ModelMap map) {
        try {
            deparmentService.deleteDepartment(id);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.failure(e.getMessage());
        }
        return JsonResult.success();
    }
}
